<?php

namespace app\admin\model;

use think\Db;
use think\Model;


class RoomCheck extends Model
{
    // 表名
    protected $name = 'room_check';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'typedata_text',
        'checktime_text',
        'createtime_text',
        'status_text',
        'cardzimage_text',
        'cardfimage_text',
        'room_info_text'
    ];
    
    public function getTypedataList()
    {
        return ['1' => __('Typedata 1'), '2' => __('Typedata 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getTypedataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['typedata']) ? $data['typedata'] : '');
        $list = $this->getTypedataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getChecktimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['checktime']) ? $data['checktime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setChecktimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    public function getCardzimageTextAttr($value,$data){
        $value = $value ? $value : (isset($data['cardzimage']) ? $data['cardzimage'] : '');
        if($value){
            $site = \think\Config::get("site");
            $preg = "/^http(s)?:\\/\\/.+/";
            if(!preg_match($preg,$value))
            {
                $value = $site['file_url'].$value;
            }
            return $value;
        }
    }
    public function getCardfimageTextAttr($value,$data){
        $value = $value ? $value : (isset($data['cardfimage']) ? $data['cardfimage'] : '');
        if($value){
            $site = \think\Config::get("site");
            $preg = "/^http(s)?:\\/\\/.+/";
            if(!preg_match($preg,$value))
            {
                $value = $site['file_url'].$value;
            }
            return $value;
        }
    }

    public function getRoomInfoTextAttr($value,$data){
        $value = $value ? $value : (isset($data['room_info']) ? $data['room_info'] : '');
        if($value){
            $site = \think\Config::get("site");
            $preg = "/^http(s)?:\\/\\/.+/";
            if(!preg_match($preg,$value))
            {
                $value = $site['file_url'].$value;
            }
        }
        return $value;
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function site()
    {
        return $this->belongsTo('User', 'site_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * Notes: 提交房产认证信息
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/18 0018
     * Time: 17:27
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _add($data){
        $site_id = $data['site_id'];
        $userModel = new User();
        $siteInfo = $userModel->where('id',$site_id)->find();
        $userInfo = $userModel->where('id',$data['user_id'])->find();
        if(empty($siteInfo)){
            $this->error='无此站点';
            return false;
        }
        if($siteInfo->group_id==2 && $siteInfo->site_level!=5){
            $this->error = '业主会员仅可认证基础站点';
        }
        if($siteInfo->group_id==2){
            $roomCheckNumber2 = $this->where('user_id',$siteInfo->id)->where('site_id',$site_id)->where('status','2')->where('typedata','1')->find();
            if($roomCheckNumber2)$this->error = '此站点已认证';
        }
        if($siteInfo->group_id>2){
            $roomCheckNumber = $this->where('user_id',$siteInfo->id)->where('status','2')->where('typedata','1')->find();
            if($roomCheckNumber)$this->error = '已认证请勿重复提交';
        }
        $data['level'] = $siteInfo->site_level;
        $data['province_id'] = $siteInfo->province_id;
        $data['city_id'] = $siteInfo->city_id;
        $data['area_id'] = $siteInfo->area_id;
        $data['town_id'] = $siteInfo->town_id;
        $data['area_str'] = $siteInfo->area_str;
        $data['sitename'] = $siteInfo->sitename;
        if($userInfo->is_check==0){
            $userInfo->is_check=2;
            $userInfo->save();
        }
        $ret = $this->allowField(true)->save($data);
        if($ret){
            return true;
        }
        $this->error='添加失败';
        return false;
    }

    /**
     * Notes: 房产认证审核未通过编辑
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/19 0019
     * Time: 17:16
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _edit($data){
        $site_id = $data['site_id'];
        $checkInfo = $this->find($data['id']);
        if(!$checkInfo){
            $this->error='无此认证信息';
            return false;
        }
        if($checkInfo->status==2){
            $this->error='审核通过不可编辑';
            return false;
        }
        if($checkInfo->status==1){
            $this->error='审核中不可编辑';
            return false;
        }
        if($checkInfo->site_id!=$data['site_id']){
            $userModel = new User();
            $siteInfo = $userModel->where('id',$site_id)->find();
            if(empty($siteInfo)){
                $this->error='无此站点';
                return false;
            }
            $data['province_id'] = $siteInfo->province_id;
            $data['city_id'] = $siteInfo->city_id;
            $data['area_id'] = $siteInfo->area_id;
            $data['town_id'] = $siteInfo->town_id;
            $data['area_str'] = $siteInfo->area_str;
            $data['sitename'] = $siteInfo->site_name;
        }
        $data['status'] = 1;
        $ret = $this->allowField(true)->save($data,['id'=>$data['id']]);
        if($ret){
            return true;
        }
        $this->error='编辑失败';
        return false;
    }

    /**
     * Notes: 删除认证
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/19 0019
     * Time: 17:31
     * @param $id
     * @return bool
     */
    public function _delete($id,$user_id){
        $checkInfo = $this->find($id);
        if($checkInfo->user_id!=$user_id){
            $this->error='仅可删除自己的认证';
            return false;
        }
        Db::startTrans();
        db('user_site')->where('user_id',$checkInfo['user_id'])->where('site_id',$checkInfo['site_id'])->delete();
        $res = $this->where('id',$id)->delete();
        $checkInfo = \db('room_check')->where('user_id',$user_id)->where('typedata','1')->where('status','2')->find();
        if(empty($checkInfo)){
            db('user')->where('id',$user_id)->setField('is_check','0');
        }
        Db::commit();
        if($res){
            return true;
        }
        $this->error='编辑失败';
        return false;
    }

}
